package com.jrtc.backend.controller;

import com.jrtc.base.config.dto.ResultDTO;
import com.jrtc.base.config.dto.ResultDTOBuilder;
import com.jrtc.base.entity.bo.CatchBallRecord;
import com.jrtc.base.entity.bo.Hospital;
import com.jrtc.base.entity.vo.CatchBallRecordVo;
import com.jrtc.service.CatchBallRecordService;
import com.jrtc.service.HospitalService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("hospital")
public class HospitalController {
    /**
     * 服务对象
     */
    @Resource
    private HospitalService hospitalService;

    /**
     * 医院列表
     * */
    @PostMapping("/hospitalList")
    public ResultDTO hospitalList(){
        List<Hospital> hospitalList = hospitalService.hospitalList();
        return ResultDTOBuilder.success(hospitalList);
    }

    /**
     * 医院唯一号获取医院信息
     * */
    @PostMapping("/hospitalByNum")
    public ResultDTO hospitalByNum(@RequestBody Map<String, Object> map){
        Integer num = Integer.parseInt(map.get("num").toString());
        Hospital hospital = hospitalService.hospitalByNum(num);
        return ResultDTOBuilder.success(hospital);
    }

    /**
     * 上传医院logo
     * */
    @PostMapping("/uploadLogo")
    public ResultDTO uploadLogo(MultipartFile file){
        String logo;
        try {
            logo = hospitalService.uploadLogo(file);
        } catch (Exception e){
            return ResultDTOBuilder.failureMsg(e.getMessage());
        }

        return ResultDTOBuilder.success(logo);
    }

    /**
     * 添加机构
     * */
    @PostMapping("/insertHospital")
    public ResultDTO insertHospital(@RequestBody Hospital hospital){
        int i;
        try {
            i = hospitalService.insertHospital(hospital);
        } catch (Exception e){
            return ResultDTOBuilder.failureMsg(e.getMessage());
        }

        if (i > 0){
            return ResultDTOBuilder.success("添加成功");
        }
        else {
            return ResultDTOBuilder.failure("添加失败");
        }
    }

}